package com.jueling.culture.dto.res;

import lombok.Data;

import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linp
 * @create 2022/4/14 20:41
 */
@Data
public class AgentUser {

    private String id;

    private String label;

    private String pCustId;

    @Transient
    private List<AgentUser> children = new ArrayList<>();

    public static List<AgentUser> parseTree(List<AgentUser> list){
        List<AgentUser> treeList=new ArrayList<AgentUser>();
        //添加一级权限
        for (AgentUser agentUser : list) {
            if("0".equals(agentUser.getPCustId())){
                treeList.add(agentUser);
            }
        }
        //递归添加所有级别的权限
        for (AgentUser parent : treeList) {
            recursiveTree(parent,list);
        }
        return treeList;
    }
    private static void recursiveTree(AgentUser parent, List<AgentUser> list){
        for (AgentUser agentUser : list) {
            if(parent.getId().equals(agentUser.getPCustId())){
                recursiveTree(agentUser,list);
                parent.getChildren().add(agentUser);
            }
        }
    }

}
